/*******************************************************************************
 * Copyright 2017 Bstek
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.urule.console;


import com.bstek.urule.console.repository.model.FileType;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.tree.DefaultAttribute;
import org.dom4j.tree.DefaultElement;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;


/**
 * @author Jacky.gao
 * @since 2016年8月30日
 */
@Component
public class UuidRepositoryInteceptor implements RepositoryInteceptor {

    public static final String UNDEFINED_STR = "undefined";
    public static final String UUID_STR = "uuid";

    @Override
    public void readFile(String file) {

    }

    @Override
    public void saveFile(String file, Document document) {
        // TODO Auto-generated method stub
        switch (FileType.endWithsName(file)) {
            case VariableLibrary:
                addUUid(document);
            default:
                return;
        }
    }

    /**
     * 添加uuid
     *
     * @param document
     */
    private void addUUid(Document document) {
        Element rootElement = document.getRootElement();
        for (Object obj : rootElement.elements()) {
            if (!(obj instanceof Element)) {
                continue;
            }
            Element element = (Element) obj;

            setClassUUid(element);
            setPropertyUUid(element);
        }
    }

    /**
     * 设置类的uuid
     *
     * @param element
     */
    private void setClassUUid(Element element) {
        List attributes = element.attributes();
        for (Object a : attributes) {
            if (uuidCondition(a)) {
                break;
            }
        }
    }

    /**
     * 设置类各字段的uuid
     *
     * @param element
     */
    private void setPropertyUUid(Element element) {
        List contents = element.content();
        for (Object a : contents) {
            DefaultElement defaultElement = (DefaultElement) a;
            for (Object attr : defaultElement.attributes()) {
                if (uuidCondition(attr)) {
                    break;
                }
            }
        }
    }

    /**
     * 判断是否需要设置uuid
     *
     * @param a
     * @return
     */
    private Boolean uuidCondition(Object a) {
        Boolean condition = false;
        DefaultAttribute defaultAttribute = (DefaultAttribute) a;
        Boolean defaultAttributeValue = UNDEFINED_STR.equals(defaultAttribute.getValue()) || StringUtils.isBlank(defaultAttribute.getValue());
        if (UUID_STR.equals(defaultAttribute.getName()) && defaultAttributeValue) {
            defaultAttribute.setValue(UUID.randomUUID().toString());
            condition = true;
        }
        return condition;
    }

    @Override
    public void createFile(String file, String content) {
        // TODO Auto-generated method stub

    }

    @Override
    public void deleteFile(String file) {
        // TODO Auto-generated method stub

    }

    @Override
    public void renameFile(String oldFileName, String newFileName) {
        // TODO Auto-generated method stub

    }

    @Override
    public void createDir(String dir) {
        // TODO Auto-generated method stub

    }

    @Override
    public void createProject(String project) {
        // TODO Auto-generated method stub

    }
}
